package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.company.DeleteCompanyBo;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.CompanyDao;
import com.zhuss.core.db.entity.Company;
import com.zhuss.core.db.entity.Dept;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 删除公司 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("companyDelete")
public class CompanyDelete extends AbstractProcessor<Object, DeleteCompanyBo> implements Processor<Object, DeleteCompanyBo> {

    @Autowired
    private CompanyDao companyDao;
    
    @Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
    
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    
    @Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
    
    
    /**
     * 校验
     */
	@Override
	@SuppressWarnings("unchecked")
    public void check(DeleteCompanyBo bo) throws AabException {
    	Long companyId = bo.getCompanyId();
    	List<Long> userIdList = (List<Long>) userRead.listUserIdByCompany(companyId);
    	if (!userIdList.isEmpty()) {
    		throw new AabException(ExceptionCode.不能删除有用户的公司);
    	}
    	List<Dept> deptList = deptRead.listByCompany(companyId);
    	if (!deptList.isEmpty()) {
    		throw new AabException(ExceptionCode.不能删除有部门的公司);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteCompanyBo param) throws AabException {
    	Long companyId = param.getCompanyId();
    	String name = ((DetailCompanyVo) companyRead.detail(companyId)).getName();
    	
    	Company record = new Company();
    	record.setCompanyId(companyId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	companyDao.delete(record);
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteCompanyBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.公司删除.key());
    	record.setTargetId(param.getCompanyId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
